/**
 * Company : Thai Informatic System Co., Ltd.
 * Created By : Dr.Tree
 * Created On : Feb 16, 2012  6:39:32 PM 
 * e-Mail : chatree@tis.co.th
 * Mobile Phone : (+66) 087-921-5115
 * Package Name : apps.promis.in.common
 * File Name : SupplierGroupMasterAction.java
 * TODO
 */
package apps.promis.in.common;

import java.util.List;

import jpa.entitys.promis.common.SupplierGroupMaster;
import jpa.entitys.synchro.staging.MmVendorMasInb;
import jpa.service.promis.common.SupplierGroupMasterServices;

import org.apache.log4j.Logger;

import promis.utility.CommonService;
import promis.utility.Utilitys;
import apps.sessionFactory;

/**
 * 
 * @author Chatree Siangklom of Administrator
 * Date =Nov 13, 2013 : 2:37:00 PM 
 * CreateBy=Chatree Siangklom
 * 
 * SupplierGroupMasterAction
 * TODO
 */
public class SupplierGroupMasterAction {
	
	private static final Logger log = Logger.getLogger(SupplierGroupMasterAction.class);

	/**
	 * 
	 * Date =Nov 13, 2013 : 2:36:57 PM 
	 * CreateBy=Chatree Siangklom
	 * SupplierGroupMasterAction
	 * TODO
	 */
	public SupplierGroupMasterAction() {
		// TODO Auto-generated constructor stub
	}
	
	/**
	 * 
	 * Date =Nov 13, 2013 : 2:37:03 PM 
	 * CreateBy=Chatree Siangklom
	 * SupplierGroupMaster
	 * TODO
	 * @param mmVendorMasInb
	 * @return
	 * @throws Exception
	 */
	public SupplierGroupMaster InsertOrUpdateMmVendorGroup(MmVendorMasInb mmVendorMasInb) throws Exception {
		SupplierGroupMasterServices supplierGroupMasterServ = new SupplierGroupMasterServices(sessionFactory.CreateEmPromis());
		
		if(mmVendorMasInb != null)
		{
			if(!CommonService.IsNull(mmVendorMasInb.getSupplierGroup()))
			{
				SupplierGroupMaster model = supplierGroupMasterServ.findByDescription(mmVendorMasInb.getSupplierGroup());
				if(model == null){
					model = new SupplierGroupMaster();

					List<SupplierGroupMaster> lst = supplierGroupMasterServ.findAll();
					if(CommonService.IsNull(lst))
						model.setSupplierGroup("01");
					else
					{
						int row = lst.size() + 1;
						if(row >=0 && row <= 9)
							model.setSupplierGroup("0"+row);
						else
							model.setSupplierGroup(row+"");
					}
					
					model.setDescription(mmVendorMasInb.getSupplierGroup());
					
					model.setSyncExternalRefKey(mmVendorMasInb.getExtRefKey());
					model.setSyncExternalRefNo(mmVendorMasInb.getExtRefNo());
					
					model.setSyncIssync("N");
					model.setCreatedBy(mmVendorMasInb.getCreatedBy());
					model.setCreatedOn(Utilitys.ConvertStringToDateTime(mmVendorMasInb.getCreatedOn()));
					
					log.info(" Insert Supplier Group = " + model.getSupplierGroup());
					return supplierGroupMasterServ.Inserted(model);
					
				}else{
					
					model.setDescription(mmVendorMasInb.getSupplierGroup());
					
					model.setSyncExternalRefKey(mmVendorMasInb.getExtRefKey());
					model.setSyncExternalRefNo(mmVendorMasInb.getExtRefNo());
					
					model.setSyncIssync("N");
					if(CommonService.IsNull(mmVendorMasInb.getUpdatedOn()))
						model.setLastUpdOn(Utilitys.ConvertStringToDateTime(mmVendorMasInb.getCreatedOn()));				
					else
						model.setLastUpdOn(Utilitys.ConvertStringToDateTime(mmVendorMasInb.getUpdatedOn()));
					
					model.setLastUpdBy(mmVendorMasInb.getCreatedBy());
					
					log.info(" Updated Supplier Group = " + model.getSupplierGroup());
					return supplierGroupMasterServ.Updated(model);
				}				
			}
		}else{
			return null;
		}
		return null;
	}
}
